//
//  JJBaseViewController.m
//  DemoPro
//
//  Created by vpjacob on 2017/4/7.
//  Copyright © 2017年 vpjacob. All rights reserved.
//

#import "JJBaseViewController.h"
#import "JJMJRefreshNoMoreDataFooter.h"
//#import "KNMainConfigModel.h"

typedef void(^JJMJFooterLoadCompleteBlock)(NSInteger page);
typedef void(^JMJHeaderLoadCompleteBlock)(NSInteger page);

@interface JJBaseViewController ()
@property (nonatomic, copy) JJMJFooterLoadCompleteBlock footerCompleteBlock;
@end

@implementation JJBaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    CGRect newFrame = CGRectMake(0, 0, JJ_SCREEN_WIDTH, JJ_SCREEN_HEIGHT);
    self.jjTableView.frame = newFrame;
    self.jjGroupTableView.frame = newFrame;
}

- (void)addRightBarItemImageName:(NSString *)imgName sel:(SEL)sel {
    NSArray *items = [self rightBarButtonImageName:imgName sel:sel leftEdg:-20];
    self.navigationItem.rightBarButtonItems = items;
}


- (NSArray *)rightBarButtonImageName:(NSString *)imgName sel:(SEL)sel leftEdg:(CGFloat)edg{
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 0, 40, 44);
    backBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [backBtn setImageEdgeInsets:UIEdgeInsetsMake(0, edg, 0, 0)];
    [backBtn setImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];
    [backBtn addTarget:self action:sel forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    UIBarButtonItem *placeHolditem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    placeHolditem.width = -8;
    return @[placeHolditem,item];
}

- (NSArray *)leftBarButtonImageName:(NSString *)imgName sel:(SEL)sel leftEdg:(CGFloat)edg{
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 0, 40, 44);
    [backBtn setImageEdgeInsets:UIEdgeInsetsMake(0, edg, 0, 0)];
    [backBtn setImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];
    [backBtn addTarget:self action:sel forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    UIBarButtonItem *placeHolditem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:nil];
    return @[item,placeHolditem];
}

- (void)addRightBarItemTitle:(NSString *)title sel:(SEL)sel {
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStylePlain target:self action:sel];
    [rightItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont boldSystemFontOfSize:12.0],NSFontAttributeName, nil] forState:UIControlStateNormal];
    [rightItem setTintColor:[UIColor knThemeColor]];
    UIBarButtonItem *placeHolditem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    placeHolditem.width = -8;
    self.navigationItem.rightBarButtonItems = @[placeHolditem,rightItem];
}

#pragma mark - UIBarButtonItemAction
- (void)leftBarButtonItemAction:(UIBarButtonItem *)item {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    return nil;
}

#pragma mark - Getting
- (UITableView *)jjTableView { //创建的tableview   plain
    if (!_jjTableView) {
        CGRect frame = CGRectMake(0, 0, JJ_SCREEN_WIDTH, JJ_SCREEN_HEIGHT - 64);
        _jjTableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain];
        _jjTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _jjTableView.showsHorizontalScrollIndicator=NO;
        _jjTableView.showsVerticalScrollIndicator=NO;
       
        _jjTableView.backgroundColor = [UIColor knBgColor];
        _jjTableView.delegate = self;
        _jjTableView.dataSource = self;
    }
    return _jjTableView;
}

- (UITableView *)jjGroupTableView { // group
    if (!_jjGroupTableView) {
        CGRect frame = CGRectMake(0, 0, JJ_SCREEN_WIDTH, JJ_SCREEN_HEIGHT - 64);
        _jjGroupTableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStyleGrouped];
        _jjGroupTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _jjGroupTableView.backgroundColor = [UIColor knBgColor];
        _jjGroupTableView.delegate = self;
        _jjGroupTableView.dataSource = self;
        _jjGroupTableView.sectionFooterHeight = CGFLOAT_MIN;
        _jjGroupTableView.sectionHeaderHeight = CGFLOAT_MIN;
    }
    return _jjGroupTableView;
}

- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (UIView *)navBgView {
    if (!_navBgView) {
        CGRect kFrame = CGRectMake(0, -64, JJ_SCREEN_WIDTH, 64);
        _navBgView = [[UIView alloc] initWithFrame:kFrame];
//        _navBgView.backgroundColor = [KNMainConfigModel shareInstance].tabNavColor;
    }
    return _navBgView;
}

#pragma mark -  MJRefresh
- (void)addMJRefreshFootView:(JJMJFooterLoadCompleteBlock)completeBlock {
    UIImage *nomoImage = [UIImage imageNamed:@"base_nomore"];
    JJMJRefreshNoMoreDataFooter *knbFooter = [JJMJRefreshNoMoreDataFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    [knbFooter setImage:nomoImage];
    self.jjTableView.mj_footer = knbFooter;
    
    JJMJRefreshNoMoreDataFooter *knGroupFooter = [JJMJRefreshNoMoreDataFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    [knGroupFooter setImage:nomoImage];
    self.jjGroupTableView.mj_footer = knGroupFooter;
    //    self.knGroupTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    //    self.knbTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    self.footerCompleteBlock = completeBlock;
}

- (void)loadMoreData {
    self.requestPage += 1;
    if (self.footerCompleteBlock) {
        self.footerCompleteBlock(self.requestPage);
    }
}

- (void)requestSuccess:(BOOL)success requestEnd:(BOOL)end {
    [self.jjGroupTableView.mj_header endRefreshing];
    [self.jjGroupTableView.mj_footer endRefreshing];
    [self.jjTableView.mj_header endRefreshing];
    [self.jjTableView.mj_footer endRefreshing];
    
    if (end) {
        [self.jjTableView.mj_footer endRefreshingWithNoMoreData];
        [self.jjGroupTableView.mj_footer endRefreshingWithNoMoreData];
        [self.jjGroupTableView reloadData];
        [self.jjTableView reloadData];
        return;
    }
    if (!success && self.requestPage > 1) {
        self.requestPage -= 1;
    }
    else {
        [self.jjGroupTableView reloadData];
        [self.jjTableView reloadData];
    }
}



- (void)addMJRefreshHeadView:(JJMJHeaderLoadCompleteBlock)completeBlock {
    JJ_WS(weakSelf);
    self.jjTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf.jjTableView.mj_footer resetNoMoreData];
        weakSelf.requestPage = 1;
        if (completeBlock) {
            completeBlock(1);
        }
    }];
    
    self.jjGroupTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf.jjGroupTableView.mj_footer resetNoMoreData];
        weakSelf.requestPage = 1;
        if (completeBlock) {
            completeBlock(1);
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
